package org.esk.dablog.web.util;

import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Properties;

/**
 * This class extends SimpleMappingExceptionResolver and provides logging facilities
 * User: esk
 * Date: 22.12.2006
 * Time: 18:26:52
 * $Id:$
 */
public class LoggingExceptionResolver extends SimpleMappingExceptionResolver {

    private Properties exceptionMappings;

    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        ModelAndView result = super.resolveException(request, response, handler, ex);

        String viewName = this.exceptionMappings != null ? findMatchingViewName(exceptionMappings, ex) : null;
        if (viewName == null) {
            logger.error("Unhandled exception", ex);
        } else {
            logger.debug("Handled exception, resolved to view name '" + viewName + "'", ex);
        }
        return result;
    }


    public void setExceptionMappings(Properties mappings) {
        super.setExceptionMappings(mappings);
        this.exceptionMappings = mappings;
    }
}
